
// ---------------------------------------------------
// | File Name   : led.c
// | Author   	 : chagn7z
// | Create Date : 2014-6-16
// ---------------------------------------------------

#include "stm32f10x.h"
#include "led.h"

void led_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	EXTI_InitTypeDef EXTI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);

	/* GPIO_B Pin 13 for LED 1*/	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_13);

	/* GPIO_B Pin 12 for LED 2*/	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_12);

	/* GPIO_B Pin 11 for LED 3*/	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_11;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_11);

	/* GPIO_B Pin 10 for LED 4*/	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_10);

	/* GPIO_B 14 Pin for Button down */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_14;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	GPIO_SetBits(GPIOB, GPIO_Pin_14);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);

	EXTI_InitStruct.EXTI_Line    = EXTI_Line14;
	EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);

	NVIC_InitStruct.NVIC_IRQChannel                   = EXTI15_10_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02; 
	NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 0x03;
	NVIC_InitStruct.NVIC_IRQChannelCmd                = ENABLE;

	NVIC_Init(&NVIC_InitStruct); 

	/* GPIO_B 15 Pin for Button menu */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	GPIO_SetBits(GPIOB, GPIO_Pin_15);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource15);

	EXTI_InitStruct.EXTI_Line    = EXTI_Line15;
	EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);

	NVIC_InitStruct.NVIC_IRQChannel                   = EXTI15_10_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02; 
	NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 0x04;
	NVIC_InitStruct.NVIC_IRQChannelCmd                = ENABLE;

	NVIC_Init(&NVIC_InitStruct); 	

	/* GPIO_C 6 Pin for Button enter */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_6;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);

	GPIO_SetBits(GPIOC, GPIO_Pin_6);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource6);

	EXTI_InitStruct.EXTI_Line    = EXTI_Line6;
	EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);

	NVIC_InitStruct.NVIC_IRQChannel                   = EXTI9_5_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02; 
	NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 0x05;
	NVIC_InitStruct.NVIC_IRQChannelCmd                = ENABLE;

	NVIC_Init(&NVIC_InitStruct); 	

	/* GPIO_C 7 Pin for Button up */	
	GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_7;
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);	
	
	GPIO_SetBits(GPIOC, GPIO_Pin_7);

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource7);

	EXTI_InitStruct.EXTI_Line    = EXTI_Line7;
	EXTI_InitStruct.EXTI_Mode    = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);

	NVIC_InitStruct.NVIC_IRQChannel                   = EXTI9_5_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02; 
	NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 0x06;
	NVIC_InitStruct.NVIC_IRQChannelCmd                = ENABLE;

	NVIC_Init(&NVIC_InitStruct); 	
}

